package the.world.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import the.world.bean.MessageList;
import the.world.bean.SortStategy;
import the.world.service.ListService;
import the.world.util.Result;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ListController {
    @Autowired
    private ListService listService;
//    展示攻略游戏分类
    @GetMapping("/home")
    public ModelAndView getCategory(@RequestParam(defaultValue = "1") int currentPage,
                                @RequestParam(defaultValue = "10") int pageSize) {
        ModelAndView mv = new ModelAndView();
        Map<String, Object> map = requestData(currentPage, pageSize);
        List<MessageList> lists = (List<MessageList>) map.get("list");
        List<SortStategy> sortStategies = (List<SortStategy>) map.get("option");
        if (!lists.isEmpty()) {
            mv.addObject("option", sortStategies);
            mv.addObject("list", lists);
            mv.addObject("totalPages", map.get("totalPages"));
        } else {
            mv.addObject("error", "出错了请联系管理人员");
        }
        mv.setViewName("/index");
        return mv;
    }
    @GetMapping("/selectByStrategyId")
    public ModelAndView selectByStrategyId(int currentPage,int pageSize,int id) {
        ModelAndView mv = new ModelAndView();
        Map<String, Object> map = requestData(currentPage, pageSize, id);
        List<MessageList> lists = (List<MessageList>) map.get("list");
        List<SortStategy> sortStategies = (List<SortStategy>) map.get("option");
        if (!lists.isEmpty()) {
            mv.addObject("option", sortStategies);
            mv.addObject("list", lists);
            mv.addObject("totalPages", map.get("totalPages"));
        } else {
            mv.addObject("error", "出错了请联系管理人员");
        }
        mv.setViewName("/index");
        return mv;
    }
    @GetMapping("/zelda/search")
    public ModelAndView selectByName(int currentPage,int pageSize,String value){
        ModelAndView mv = new ModelAndView();
        Map<String, Object> map = selectData(currentPage, pageSize, value);
        List<MessageList> lists = (List<MessageList>) map.get("list");
        List<SortStategy> sortStategies = (List<SortStategy>) map.get("option");
        if (!lists.isEmpty()) {
            mv.addObject("option", sortStategies);
            mv.addObject("list", lists);
            mv.addObject("totalPages", map.get("totalPages"));
        } else {
            mv.addObject("error", "出错了请联系管理人员");
        }
        mv.setViewName("/index");
        return mv;
    }
    private Map<String, Object> requestData(int currentPage, int pageSize) {
        List<SortStategy> sortStategies = listService.listAllByList();
        List<MessageList> list = listService.listAllByOption(currentPage, pageSize);
        int totalCount = listService.totalCount();
        int totalPages = (int) Math.ceil((double) totalCount / pageSize);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("option", sortStategies);
        map.put("totalPages", totalPages);
        return map;
    }
    private Map<String, Object> requestData(int currentPage, int pageSize, int id) {
        List<SortStategy> sortStategies = listService.listAllByList();
        List<MessageList> list = listService.selectByStrategyId(currentPage, pageSize, id);
        int totalCount = listService.totalCount();
        int totalPages = (int) Math.ceil((double) totalCount / pageSize);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("option", sortStategies);
        map.put("totalPages", totalPages);
        return map;
    }
    private Map<String, Object> selectData(int currentPage, int pageSize, String value) {
        List<SortStategy> sortStategies = listService.listAllByList();
        List<MessageList> list = listService.selectByName(currentPage, pageSize, value);
        int totalCount = listService.totalCount();
        int totalPages = (int) Math.ceil((double) totalCount / pageSize);
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("option", sortStategies);
        map.put("totalPages", totalPages);
        return map;
    }
}